<template>
	<view class="shopcart">
		<view>
			<u-toast ref="uToast" />
		</view>
		<view class="bg">
			<view class="search-container">
				<view class="search">
					<input class="ipt" type="text" placeholder="请输入搜索关键字" v-model="keyword">
				</view>
				<view class="submit" @click="search">
					<text>搜索</text>
				</view>
			</view>
		</view>

		<view class="cart">
			<!-- 购物车商品列表 -->
			<view v-if="isLogin" class="is-login">
				<view class="empty" v-if="isEmpty">
					<text v-if="keyword == ''">
						空空的购物车~
						喜欢什么就快去采购吧
					</text>
					<u-empty text="没有搜索结果" mode="list" v-else></u-empty>
				</view>

				<view v-else class="shop-list">
					<view class="shop-item" v-for="item in shopList" :key="item.orderUuid">
						<view class="check">
							<u-checkbox v-model="item.checked" :name="item.orderUuid" active-color="#FC5E14"
								@change="checkcur">
							</u-checkbox>
						</view>
						<view class="pro-img">
							<image :src="item.productImg" mode="widthFix"></image>
						</view>
						<view class="product-info">
							<view class="title">
								{{item.productTitle}}
							</view>
							<view class="guige">
								{{item.formatName}}
							</view>
							<view class="total-price">
								<text class="price">￥{{(item.productMoney / 100).toFixed(2)}}</text>
								<text class="num">×{{item.productNum}}件</text>
							</view>
							<view class="remark">
								{{item.bayInfo}}
							</view>
							<view class="del" @click="delCurPro(item.orderUuid)">
								<view class="iconfont icon-shanchu"></view>
							</view>

						</view>
					</view>
				</view>
			</view>
			<view class="login" v-else @click="login">
				登录
			</view>

		</view>

		<GifPic></GifPic>

		<view class="recommend-product">
			<view class="title">
				<text class="text">推荐商品</text>
			</view>
			<view class="product-list">
				<CartRecommend></CartRecommend>
			</view>

		</view>

		<!-- 去结算 -->
		<view class="gopay">
			<view class="check-all">
				<u-checkbox v-model="checkAll" active-color="#007AFF" label-size="12px" @change="checkAllPro">
					<view class="text">
						全选
					</view>
				</u-checkbox>

			</view>
			<view class="toalprice">
				￥{{totalPrice}}
			</view>
			<view class="paybtn" @click="goPay">
				去结算
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getLocalData,
		setLocalData
	} from '@/util/localStroage.js'
	import request from '@/util/request.js'
	import GifPic from '@/components/index/GifPic/GifPic.vue'
	import CartRecommend from "@/components/cart/CartRecommend/CartRecommend.vue"
	export default {
		data() {
			return {
				keyword: '',
				checkAll: false,
				isLogin: false,
				shopList: []
			}
		},
		components: {
			GifPic,
			CartRecommend
		},
		computed: {
			isEmpty() {
				if (this.shopList.length == 0) {
					return true
				}

				return false
			},
			totalPrice() {
				const price = this.shopList.reduce((total, item) => {
					if (item.checked) {
						return total += item.productMoney
					} else {
						return total
					}

				}, 0)
				if (price == 0) {
					return '0.00'
				} else {
					return (price / 100).toFixed(2)
				}
			}
		},
		methods: {
			checkcur(detail) {
				if (!detail.value) {
					this.checkAll = false
					return
				}
				let s = true
				this.shopList.some(item => {
					if (item.orderUuid != detail.name && !item.checked) {
						s = false
						return true
					}
				})
				this.checkAll = s
			},
			checkAllPro(detail) {
				this.shopList.forEach(item => {
					item.checked = detail.value
				})
			},
			showRes(title, type, callback) {
				this.$refs.uToast.show({
					title,
					type: type ? type : "default",
					duration: 1000,
					position: "top",
					callback: callback ? callback : () => {}
				})
			},
			// 请求用户购物扯数据列表
			requestShopList() {
				const usertoken = uni.getStorageSync('usertoken')
				if (usertoken) {
					this.isLogin = true

					request.globalRequest({
						url: "/api/mall/product/car_list",
						data: {
							key: this.keyword
						},
						headers: {
							userType: "MEMBER"
						}
					}).then(res => {
						res.forEach(pro => {
							this.shopList.push({
								bayInfo: pro.bayInfo,
								formatName: pro.formatName,
								orderUuid: pro.orderUuid,
								productImg: 'http://yihao.cdn.gd-zjgj.com' + pro.productImg,
								productTitle: pro.productTitle,
								productMoney: pro.productMoney,
								productNum: pro.productNum,
								checked: false
							})
						})
					}).catch(error => {})
				}
			},
			login() {
				uni.navigateTo({
					url: '/pages/login/login'
				})
			},
			// 删除购物车数据
			delCurPro(uuid) {
				request.globalRequest({
					url: '/api/mall/product/order_car_del',
					method: "post",
					data: {
						uuid
					},
					headers: {
						userType: 'MEMBER'
					}
				}).then(res => {
					if (res.status == "0") {
						this.showRes("删除成功", "success", () => {
							this.shopList = []
							this.requestShopList()
						})
					}
				})
			},
			// 通过关键字搜索购物车商品
			search() {
				this.shopList = []
				this.requestShopList()
			},
			setId() {
				const date = new Date()
				return "" + date.getFullYear() + (date.getMonth() + 1) + date.getDate() + date.getTime()
			},
			// 支付
			goPay() {
				const arr = this.shopList.filter(item => item.checked)
				if (arr.length == 0) {
					return this.showRes("请选择需要购买的商品")
				}
				let data = getLocalData("orderList")
				const id = this.setId()
				let proList = []
				arr.forEach(item => {
					proList.push({
						title: item.productTitle,
						image: item.productImg,
						count: item.productNum,
						uuid: item.orderUuid,
						remark: item.bayInfo,
						format: item.formatName,
						sellPrice: item.productMoney
					})
					this.delCurPro(item.orderUuid)
				})
				const order = {
					id: id,
					status: 1,
					proList: proList
				}
				data.unshift(order)
				this.remark = ''
				setLocalData("orderList", data)
				uni.navigateTo({
					url: "/pages/orderDetail/orderDetail?id=" + id
				})
			}
		},
		onShow() {
			this.shopList = []
			this.requestShopList()
		}
	}
</script>

<style lang="scss" scoped>
	.shopcart {
		padding: 0 10rpx 100rpx;

		.bg {
			height: 100rpx;
			padding: 0 10rpx;
			position: relative;
			background-image: url('@/static/image/main.png');
			background-repeat: no-repeat;
			background-size: 100% auto;
			padding-top: 20rpx;

			.search-container {
				height: 70rpx;
				background-color: #fff;
				border-radius: 999rpx;
				padding: 0 20rpx;
				display: flex;
				justify-content: space-between;
				align-items: center;

				.search {
					padding-left: 10rpx;
					flex: 1;

					.ipt {
						font-size: 24rpx;
						color: #000;
					}
				}

				.submit {
					background-color: #FC5E14;
					color: #fff;
					font-size: 24rpx;
					line-height: 50rpx;
					padding: 0 30rpx;
					border-radius: 999rpx;
				}
			}

		}

		.cart {
			margin-top: 30rpx;
			min-height: 140rpx;
			display: flex;
			align-items: center;
			justify-content: center;

			.empty {
				display: flex;
				align-items: center;
				justify-content: center;
				color: #7E7E7E;
				font-size: 32rpx;

				text {
					text-align: center;
				}
			}

			.login {
				background-color: #FC5E14;
				color: #fff;
				font-size: 28rpx;
				font-weight: 700;
				border-radius: 999rpx;
				padding: 10rpx 80rpx;
				display: inline-block;
			}


			.is-login {
				width: 100%;
			}

			.shop-list {

				.shop-item {
					display: flex;
					width: 100%;
					padding: 20rpx 30rpx;
					align-items: center;
					background-color: #fff;
					margin-bottom: 20rpx;


					.pro-img {
						width: 140rpx;
						border-radius: 10rpx;
						overflow: hidden;
						border: 1px solid #ccc;
						margin-right: 20rpx;

						image {
							width: 100%;
						}
					}

					.product-info {
						flex: 1;
						line-height: 40rpx;
					}

					.title,
					.remark {
						white-space: nowrap;
						overflow: hidden;
						text-overflow: ellipsis;
						max-width: 440rpx;
					}

					.title {
						color: #7E7E7E;
						font-size: 30rpx;

					}

					.guige {
						background-color: #EFEFF4;
						color: #7E7E7E;
						font-size: 26rpx;
						padding: 0 10rpx;
						margin: 10rpx 0;
						border-radius: 10rpx;
					}

					.total-price {
						font-size: 28rpx;

						.price {
							color: red;
							font-weight: 700;
						}

						.num {
							color: #7E7E7E;
						}
					}

					.remark {
						color: #BCA909;
						font-size: 28rpx;
						line-height: 40rpx;
						margin-bottom: 10rpx;

					}
				}


			}
		}

		.recommend-product {
			margin-top: 20rpx;

			.title {
				color: #7E7E7E;
				font-size: 24rpx;
				text-align: center;

				.text {
					position: relative;

				}

				.text::after,
				.text::before {
					content: '';
					display: block;
					position: absolute;
					top: 50%;
					transform: translateY(-50%);
					width: 120rpx;
					height: 4rpx;
					background-color: rgba(124, 124, 124, .1);
				}

				.text::after {
					left: -140rpx;
				}

				.text::before {
					right: -140rpx;
				}

			}

			.product-list {
				margin-top: 30rpx;
				padding-bottom: 20rpx;
			}
		}

		.gopay {
			position: fixed;
			width: 95%;
			margin: 0 auto;
			left: 50%;
			transform: translateX(-50%);
			bottom: 100rpx;
			background-color: #FC5E14;
			border-radius: 999rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;
			margin-bottom: 20rpx;
			padding: 0 20rpx;
			color: #fff;
			font-size: 28rpx;

			.check-all {
				.text {
					color: #fff;
				}
			}


			.toalprice,
			.paybtn {
				padding: 20rpx;
			}

			.paybtn {

				border-left: 1px solid #fff;

			}
		}
	}
</style>
